/**
  * Copyright @ 2018 HoldTime Inner Mongolia Co. Ltd.
  */
package com.holdtime.imagex.controller;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.net.URL;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/** 
 * 项目名称： ImageX<br>
 * 功能描述： ImageXController概述：<br>
 * 创  建 人： guoai<br>
 * 创建日期： 2018年6月7日<br>
 * 修  改 人：  <br>
 * 修改时间：<br>
 * 修改内容：<br>
 * 版      本： 1.0.0
 */
@Controller
public class ImageXController {

    private static Logger logger = LoggerFactory.getLogger(ImageXController.class);

    @GetMapping("zoom")
    public ResponseEntity<byte[]> zoom(@RequestParam("url") String url, @RequestParam("w") int width, @RequestParam("h") int heigth) {
        try {
            BufferedImage buffimg = ImageIO.read(new URL(url));
            BufferedImage result = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
            result.getGraphics().drawImage(buffimg.getScaledInstance(width, heigth, Image.SCALE_SMOOTH), 0, 0, null);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(result, "JPEG", outputStream);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_JPEG);
            return new ResponseEntity<byte[]>(outputStream.toByteArray(), headers, HttpStatus.CREATED);
        } catch (Exception e) {
            logger.error("转换图片异常", e);
        }
        return new ResponseEntity<byte[]>(HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
